package com.assets.util;

import android.util.Pair;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Observable;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;


public class NetworkUtil {

    private static HttpServer httpServer;

    private static HttpServer getHttpServer(String baseUrl) {
        if (httpServer == null) {
            OkHttpClient client = new OkHttpClient
                    .Builder()
                    .connectTimeout(500, TimeUnit.SECONDS)
                    .readTimeout(500, TimeUnit.SECONDS)
                    .build();
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                    .client(client)
                    .build();
            httpServer = retrofit.create(HttpServer.class);
        }
        return httpServer;
    }

    public static Observable<ResponseBody> loadData(String baseUrl, String userId, String checkId) {
        return getHttpServer(baseUrl).loadData(userId, checkId);
    }

    public static Observable<ResponseBody> updatePlace(String baseUrl, Map<String, String> map) {
        return getHttpServer(baseUrl).updatePlace(map.get("data"));
    }

    public static Observable<ResponseBody> profitDataSubmit(String baseUrl, Map<String, String> map) {
        return getHttpServer(baseUrl).profitDataSubmit(map.get("data"), map.get("id"));
    }

    public static Observable<ResponseBody> submit(String baseUrl, Map<String, String> map) {
        return getHttpServer(baseUrl).submit(map.get("data"), map.get("id"));
    }

    public static Observable<ResponseBody> rfidProfit(String baseUrl, Map<String, String> map) {
        return getHttpServer(baseUrl).rfidProfit(map.get("checkId"), map.get("rfid"), map.get("assetNumber"));
    }

//    public static Observable<ResponseBody> getVersion(String baseUrl, Map<String, String> map) {
//        return getHttpServer(baseUrl).rfidProfit(map.get("checkId"), map.get("rfid"), map.get("assetNumber"));
//    }

    public static Observable<ResponseBody> getVersion(String baseUrl) {
        return getHttpServer(baseUrl).getVersion();
    }

    public static Flowable<ResponseBody> updateApp(String baseUrl) {
        return getHttpServer(baseUrl).updateApp();
    }

    public static Observable<Pair<ResponseBody, ResponseBody>> submit1(String baseUrl, Map<String, String> submitMap, Map<String, String> profitSubmit) {
        HttpServer httpServer = getHttpServer(baseUrl);
        // 创建两个 Observable 对象：一个用于 loadData，一个用于 updatePlace
        Observable<ResponseBody> profitDataSubmit = httpServer.profitDataSubmit(profitSubmit.get("data"), profitSubmit.get("id"));
        Observable<ResponseBody> submit = httpServer.submit(submitMap.get("data"), submitMap.get("id"));

        // 使用 zip 操作符同时请求两个接口，并合并它们的结果
        return Observable.zip(
                profitDataSubmit,
                submit,
                (firstResponse, secondResponse) -> new Pair<>(firstResponse, secondResponse)
        );
    }

}
